<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if (!ispost())
{
    exit();
}
if (!isset($_SESSION['crtime']))
{
    $_SESSION['crtime'] = systime();
}
else if ($_SESSION['crtime'] == systime())
{
    exit();
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show3D3S = $chk->show3D3S;
$g_betname_3or5 = judge_type($gametype);
$show5N = getShow35($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
require_once( "class_3d/Bet_3d.php" );
require_once( "class_3d/RateConfig_3d.php" );
require_once( "class_3d/SRate_3d.php" );
require_once( "class_3d/MyLotto_3d.php" );
require_once( "class_3d/Amounts_3d.php" );
include( "class_3d/class.lotto3d.php" );
$lotto3d = new lotto3d($gametype);
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
$RateConfig_3d = new RateConfig_3d($gametype);
$SRate_3d = new SRate_3d($gametype);
if ($show5N)
{
    $MyLotto_3d = new MyLotto_3d_5n($gametype);
}
else
{
    $MyLotto_3d = new MyLotto_3d($gametype);
}
$Amounts_3d = new Amounts_3d($gametype);
$Bet_3d = new Bet_3s($gametype);
$amountField = "amount";
$amountFloat = min(0, $Bet_3d->getAmountFloat($curruser['id']));
$curruser[$amountField] += $amountFloat;
//2013-11-20
/*switch ($show3D3S)
{
    case 0 :
        $Bet_3d = new Bet_3d($gametype);
        $amountField = "amount_3d";
        break;
    case 1 :
        $Bet_3d = new Bet_3s($gametype);
        $amountField = "amount_3s1";
        $amountFloat = min(0, $Bet_3d->getAmountFloat($curruser['id']));
        $curruser[$amountField] += $amountFloat;
        break;
}*/
$today = date("Y-m-d");
$bettypegroup = getvar("palygroup");
$betmin = intval($conf->get("betmin_{$tbl}"));
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("帐号已被暂停使用");
    exit();
}
else if (1 < $curruser['acl'])
{
    $ui->showErrorPage("非会员无法下注");
    exit();
}
$disk = intval(getvar("disk"));
if (!isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] != 1)
{
    $ui->showErrorPage("盘类错误");
    exit();
}
unset($_POST['disk']);
$diskStr = $RateConfig_3d->diskArr[$disk];
$diskStr2 = $g_disk_name[$disk];
$tempArr = array("g3" => "g3", "g6" => "g6");
if (!isset($tempArr[$bettypegroup]))
{
    $ui->showErrorPage("不正确的玩法页面");
    exit();
}
unset($tempArr);
$showSubid = getvar("subid");
unset($_POST['subid']);
include_once( "BetChk.php" );
$BetChk = new BetChk($today, $gametype);
if ($lotto_3d = $BetChk->chkBetDate($today))
{
    $paperOpen = $lotto_3d['open'];
    $lotto_id = $lotto_3d['id'];
    $round = $lotto_3d['round'];
    if ($bettypegroup == "g3")
    {
        $cutNum = 19;
    }
    else
    {
        $cutNum = 26;
    }
    if (!isset($RateConfig_3d->subArr[$showSubid]))
    {
        $timeTemp = rand(10, 99) . "1" . rand(0, 9);
        $errMsg = "({$tbl})[betg3do]No.{$timeTemp} 会员下注(GD):不正确的subid,idG-{$bettypegroup},subid-{$showSubid}";
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
        $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
        exit();
    }
    if (0 < $curruser[$amountField])
    {
        $winMoney = $Bet_3d->getSumWinMoney($curruser['id'], $lotto_id, $paperOpen);
        $curruser[$amountField] += $winMoney;
    }
    $showBetTable = array();
    $rateArr = array();
    $amountsDataArr = array();
    $u2 = get_user($curruser['prev_id'], $gametype);
    $u3 = get_user($u2['prev_id'], $gametype);
    $u4 = get_user($u3['prev_id'], $gametype);
    $u5 = get_user($u4['prev_id'], $gametype);
    $mpercent = $u2['mpercent'];
    $percent2 = $u2['percent0'];
    $percent3 = min($u3['percent1'], $u4['mpercent'] - $mpercent - $percent2);
    $percent4 = min($u4['percent1'], $u5['mpercent'] - $mpercent - $percent2 - $percent3);
    $upercent = 100 - $mpercent - $percent2 - $percent3 - $percent4;
    $fields = array();
    $fields['percent2'] = $mpercent;
    $fields['percent3'] = $percent2;
    $fields['percent4'] = $percent3;
    $fields['percent5'] = $percent4;
    $fields['percent255'] = $upercent;
    $check_percent = $fields['percent255'];
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
	$usedamount=0;
    foreach ($_POST as $key => $val)
    {
        $betOKFlag = true;
        $val = intval($val);
        if (empty($val) || $val < 1)
        {
            $betOKFlag = false;
            continue;
        }
        $dataArr = explode("_", substr($key, 1));
        if (count($dataArr) != 3)
        {
            $betOKFlag = false;
            continue;
        }
        if ($dataArr[2] != $showSubid)
        {
            $betOKFlag = false;
            continue;
        }
        $betSubIdx = $dataArr[0] . "_" . $dataArr[2];
        if (!in_array($dataArr[0], $RateConfig_3d->playGroupArr[$bettypegroup]))
        {
            $betOKFlag = false;
            $timeTemp = rand(10, 99) . "2" . rand(0, 9);
            $errMsg = "({$tbl})[betg3do]No.{$timeTemp} 会员下注G:不正确的玩法:id-{$dataArr[0]}, subid-{$dataArr[2]}";
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
            exit();
        }
        $allG3Flag = $dataArr[0] == "29" ? true : false;
        if ($allG3Flag)
        {
            $valStr = "全包";
            $valNum = $dataArr[1];
            $numberArr = $valNum;
            if (!$RateConfig_3d->checkBetNumber($dataArr[0], $dataArr[1]))
            {
                $betOKFlag = false;
                $timeTemp = rand(10, 99) . "3" . rand(0, 9);
                $errMsg = "({$tbl})[betg3do]No.{$timeTemp} 会员下注G:不正确的内容:id-{$dataArr[0]},num-{$valNum},subid-{$dataArr[2]}";
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
                exit();
            }
        }
        else
        {
            $numberArr = $RateConfig_3d->mystr_split($dataArr[1], 1);
            sort($numberArr);
            $valStr = implode(",", $numberArr);
            $valNum = implode("", $numberArr);
            foreach ($numberArr as $val2)
            {
                if (!$RateConfig_3d->checkBetNumber($dataArr[0], (string) $val2))
                {
                    $betOKFlag = false;
                    $timeTemp = rand(10, 99) . "4" . rand(0, 9);
                    $errMsg = "({$tbl})[betg3do]No.{$timeTemp} 会员下注G:不正确的内容:id-" . $dataArr[0] . ",num-" . $val2 . ",subid-" . $dataArr[2];
                    write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                    $ui->showErrorPage("下注失败。(errorNo.{$timeTemp})");
                    exit();
                }
            }
            $coutVal = count($numberArr);
            $checkNumMust = $dataArr[0] - $cutNum;
            if ($coutVal < $checkNumMust)
            {
                $betOKFlag = false;
                $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => "<font class=\"red\">勾选号码不足，无法下注</font>", "betflag" => false);
                continue;
            }
            if ($checkNumMust < $coutVal)
            {
                $betOKFlag = false;
                $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => "<font class=\"red\">勾选号码过多，无法下注</font>", "betflag" => false);
                continue;
            }
        }
        if (!isset($amountsDataArr[$betSubIdx]))
        {
            $amountsData = $Amounts_3d->getAmountsDataById($dataArr[0], $curruser['id'], $dataArr[2]);
            if ($amountsData == false)
            {
                $betOKFlag = false;
                $errMsg = "({$tbl})会员下注G:无该玩法之下注限额资料:玩法id-" . $dataArr[0] . "subid-" . $dataArr[2];
                write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
                $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "无限额资料");
                continue;
            }
            $amountsDataArr[$betSubIdx] = $amountsData;
        }
        $amount1 = $amountsDataArr[$betSubIdx]['amount1'];
        $amount2 = $amountsDataArr[$betSubIdx]['amount2'];
        $amount1 = $amount1 ? $amount1 : "0";
        $amount2 = $amount2 ? $amount2 : "0";
        //$usedamount = $Bet_3d->getSumAmount($curruser['id'], $lotto_id, $paperOpen);
        //$usedamount = $usedamount ? $usedamount : "0";
        if ($val < $betmin)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能小於最低限额:" . $betmin);
            continue;
        }
        if ($curruser[$amountField] < $val + $usedamount)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过信用额度");
            continue;
        }
        if ($curruser['usemaxamount'] && $curruser['maxamount'] < $val + $usedamount)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过本期限额");
            continue;
        }
        if ($amount1 < $val)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过单注限额");
            continue;
        }
        $ret = $Amounts_3d->check_Amount_255($lotto_id, $dataArr[0], $numberArr, $val * $upercent / 100, $dataArr[2]);
        if (count($ret))
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => implode(",", $ret) . "号,总额度已满,无法下注.");
            continue;
        }
        $tempRateBaseArr = $SRate_3d->getRates($lotto_id, $disk, $dataArr[0], $dataArr[2]);
        if ($tempRateBaseArr == false)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "无赔率资料");
            $errMsg = "({$tbl})会员下注G:无该玩法赔率资料:id-" . $dataArr[0] . "subid-" . $dataArr[2];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        $user_id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
        if ($tempRateBaseArr2 = $SRate_3d->getRatesS($lotto_id, $dataArr[0], $user_id, $dataArr[2]))
        {
            $tempRateArr = array();
            foreach ($tempRateBaseArr as $key2 => $val2)
            {
                $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
            }
            $rateArr[$betSubIdx] = $tempRateArr;
            $rate5Flag = 1;
            $rate5 = 0;
        }
        else
        {
            $rateArr[$betSubIdx] = $tempRateBaseArr;
            $rate5Flag = 0;
            $rate5 = 0;
        }
        if ($allG3Flag)
        {
            $rate = $RateConfig_3d->getRateFromRateArr($dataArr[0], $dataArr[1], $rateArr[$betSubIdx]);
            if ($rate5Flag)
            {
                $rate5 = $RateConfig_3d->getRateFromRateArr($dataArr[0], $dataArr[1], $tempRateBaseArr2);
            }
        }
        else
        {
            $rate = 1000000;
            if ($rate5Flag)
            {
                $rate5 = 1000000;
            }
            foreach ($numberArr as $val2)
            {
                $rateTemp = $RateConfig_3d->getRateFromRateArr($dataArr[0], $val2, $rateArr[$betSubIdx]);
                $rate = min($rate, $rateTemp);
                if ($rate5Flag)
                {
                    $rate5Temp = $RateConfig_3d->getRateFromRateArr($dataArr[0], $val2, $tempRateBaseArr);
                    $rate5 = min($rate5, $rate5Temp);
                }
            }
            if ($rate5Flag)
            {
                $rate5 = $rate5 - $rate;
            }
        }
        if ($rate <= 1)
        {
            $betOKFlag = false;
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => "<font class=\"red\">勾选号码中有错误的赔率值，无法下注</font>", "betflag" => false);
            $errMsg = "({$tbl})会员下注G:赔率值错误:id-" . $dataArr[0] . ",num-" . $valStr . ",subid-" . $dataArr[0];
            write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, $errMsg);
            continue;
        }
        if ($betOKFlag)
        {
            $fields['user_id'] = $curruser['id'];
            $fields['lotto_id'] = $lotto_id;
            $fields['bettype'] = $dataArr[0];
            $fields['subid'] = $dataArr[2];
            $fields['numbers'] = "'" . $valNum . "'";
            $fields['amount'] = $val;
            $fields['rate'] = $rate;
            $fields['rate5'] = $rate5;
            $fields['payoff'] = $val * ( $rate - 1 );
            $fields['disk'] = $disk;
            $fields['line_nums'] = "'" . $valStr . "'";
            $fields['unitmoney'] = $val;
            $fields['rame'] = "0";
            $fields['State'] = "0";
            $fields['ip'] = "'" . getIP() . "'";
            $fields['back1'] = $Amounts_3d->getBackArr($dataArr[0], $curruser['id'], $disk, $dataArr[2]);
            $fields['back2'] = $Amounts_3d->getBackArr($dataArr[0], $u2['id'], $disk, $dataArr[2]);
            $fields['back3'] = $Amounts_3d->getBackArr($dataArr[0], $u3['id'], $disk, $dataArr[2]);
            $fields['back4'] = $Amounts_3d->getBackArr($dataArr[0], $u4['id'], $disk, $dataArr[2]);
            $fields['back5'] = $Amounts_3d->getBackArr($dataArr[0], $u5['id'], $disk, $dataArr[2]);
            if ($u5['supe_account'] != "")
            {
                $rs = getdb("select id from k_user where acl=255 and account='" . $u5['supe_account'] . "'");
                $uid = $rs->f['id'];
                $rs->close();
                $fields['back6'] = $Amounts_3d->getBackArr($dataArr[0], $uid, $disk, $dataArr[2]);
            }
            else
            {
                $fields['back6'] = "0";
            }
            if ($fields['percent255'] < 0)
            {
                $ui->showErrorPage("系统参数严重错误！");
                exit();
            }
            $fields['r2'] = $val * $fields['percent3'] / 100;
            $fields['r3'] = $val * $fields['percent4'] / 100;
            $fields['r4'] = $val * $fields['percent5'] / 100;
            $fields['r6'] = $val * $fields['percent255'] / 100;
            $fields['createtime'] = systime();
            $fields['sessid'] = "'" . enBetSessid($fields) . "'";
            $_SESSION['crtime'] = $fields['createtime'];
            $numberamount = $Bet_3d->getSumBettypeAmount($curruser['id'], $lotto_id, $dataArr[0], $dataArr[2]);
            if ($amount2 < $val + $numberamount)
            {
                $betOKFlag = false;
                $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => "--", "betmoney" => $val, "betflag" => false, "note" => "下注金额不能超过单项限额");
                continue;
            }
            $insertFlag = $Bet_3d->dbInsert($fields);
            $insertId = "";
            $oldrate = getvar("rate" . $dataArr[0] . "v" . $dataArr[1] . "v" . $dataArr[2]);
            if ($insertFlag == false)
            {
                $note = "<font color=\"red\">下注失败</font>";
            }
            else if ($oldrate != "" && $oldrate != $rate)
            {
                $note = "<font color=\"red\">赔率已更新</font>";
                $insertId = $insertFlag;
                $insertFlag = true;
            }
            else
            {
                $note = "";
                $insertId = $insertFlag;
                $insertFlag = true;
            }
            $showBetTable[] = array("bettype" => $g_betname_3or5[$betSubIdx], "nums" => $valStr, "rate" => $rate, "betmoney" => $val, "betflag" => $insertFlag, "note" => $note);
            if ($insertId !== "")
            {
                $lotto3d->getBet_3D($insertId);
            }
			$usedamount+=$val;
        }
    }
    $ui->header();
    echo "<table  class=\"tblMy3D\">";
    echo "<thead><tr class=\"title0 bgImg_" . $gametype . "\"><th colspan=\"7\">" . $tblName . " 第" . $round . "期 您下注的是：</th></tr>";
    echo "<tr class=\"bgClc_" . $gametype . "\"><th>下注方式</th><th>下注内容</th><th>盘类</th><th>赔率</th><th>下注金额</th><th>下注结果</th><th>说明</th></tr></thead>";
    echo "<tbody>";
    foreach ($showBetTable as $val)
    {
        if ($val['betflag'])
        {
            $tdMsg = "<font color=green>成功</font>";
            $classTemp = "td2";
        }
        else
        {
            $tdMsg = "失败";
            $classTemp = "red";
        }
        echo "<tr class=\"" . $classTemp . "\">";
        echo "<td>" . $val['bettype'] . "</td>";
        echo "<td>" . $val['nums'] . "</td>";
        echo "<td>" . $diskStr2 . "</td>";
        echo "<td>" . $val['rate'] . "</td>";
        echo "<td align=\"right\">" . $val['betmoney'] . "</td>";
        echo "<td>";
        echo $tdMsg;
        echo "</td>";
        echo "<td>" . $val['note'] . "</td>";
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
    $timebtnType = getvar("timebtnType") == "checkbox" ? "checkbox" : "text";
    echo "<script>";
    echo "parent.leftFrame.location.replace('op.php?op=member_3d&fp=account&gametype={$gametype}&disk={$disk}');";
    echo "setTimeout(\"window.top.mainFrame.location.replace('op.php?op=member_3d&fp=bet&palygroup={$bettypegroup}&timebtnType={$timebtnType}&gametype={$gametype}&disk={$disk}&subid={$showSubid}');\",2000);";
    echo "</script>";
    $ui->footer();
}
?>